package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

  @Autowired
  private ApArticleMapper apArticleMapper;

  @Autowired
  private ApArticleConfigMapper apArticleConfigMapper;;

  @Autowired
  private ApArticleContentMapper apArticleContentMapper;

  private final  static short Max_Page_Size = 50;

    @Override
    public ResponseResult load(ArticleHomeDto dto, Short type) {

        //参数校验
        //分页参数校验
        Integer size = dto.getSize();
        if(dto.getSize()==null || dto.getSize()<=0 || dto.getSize()>Max_Page_Size){
            dto.setSize(10);
        }
        //分页的值不能过50
        size=Math.min(dto.getSize(), Max_Page_Size);
        //参数type校验，1加载更多，2加载最新
        if(!type.equals(1)&&!type.equals(2)){
            type=1;
        }
       //频道参数校验
        if(StringUtils.isEmpty(dto.getTag())){
            dto.setTag(ArticleConstants.DEFAULT_TAG);
        }


        List<ApArticle> apArticles = apArticleMapper.loadArticleList(dto, type);

        return ResponseResult.okResult(apArticles);
    }

    /*
    *
    * 保存app端相关文章
    * */
    @Override
    public ResponseResult saveArticle(ArticleDto dto) {
      //1参数校验
     if(dto==null){
         return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
     }
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto,apArticle);
        //判断是否存在id
    if(dto.getId()==null){
      //不存在保存文章，文章配置，文章内容
      save(apArticle);
      //保存文章配置
        ApArticleConfig apArticleConfig = new ApArticleConfig(apArticle.getId());
        apArticleConfigMapper.insert(apArticleConfig);
        //保存文章内容
        ApArticleContent apArticleContent = new ApArticleContent();
        apArticleContent.setArticleId(apArticle.getId());
        apArticleContent.setContent(dto.getContent());
         apArticleContentMapper.insert(apArticleContent);

    }else{
        //存在修改文章，修改文章内容

        //修改文章
        updateById(apArticle);

        //修改文章内容
    ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, dto.getId()));
    apArticleContent.setContent(dto.getContent());
    apArticleContentMapper.updateById(apArticleContent);

    }





        //结果返回，文章id
        return ResponseResult.okResult(apArticle.getId());
    }
}
